<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/09/11 22:13:05 $)
// $RCSfile: functions_alertrulesui.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Render the Alert Rule form
*/
function renderAlertRuleForm($type)
{
	global $dbCore, $_SWIFT;

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["insertalert"]);

	printTextRow("title", $_SWIFT["language"]["ruletitle"], $_SWIFT["language"]["desc_ruletitle"], "text", $_POST["title"]);

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["email"];
	$options[0]["value"] = ALERT_EMAIL;
	$options[0]["selected"] = true;
	$options[1]["title"] = $_SWIFT["language"]["sms"];
	$options[1]["value"] = ALERT_SMS;

	printSelectRow("alerttype", $_SWIFT["language"]["alerttype"], $_SWIFT["language"]["desc_alerttype"], $options);

	$rulescope = false;
	if ($type == INSERT)
	{
		$rulescope = true;
	} else {
		if ($_POST["alertscope"] == "1")
		{
			$rulescope = true;
		}
	}

	if($type == INSERT && $_SWIFT['staff']['tcrearule'] == 0 && $_SWIFT["staff"]["isadmin"] != 1){
		$data .= '<label for="alertscopeno">'. '<input type="radio" name="alertscope" id="alertscopeno" value="0" checked" />'.$_SWIFT["language"]["private"].'</label>'.SWIFT_CRLF;
		$info = '<span class="tabletitle">'. $_SWIFT["language"]["alertscope"] .'</span><BR /><span class="tabledescription">'. $_SWIFT["language"]["desc_alertscope"] .'</span>';

		printDefaultRow($info, $data);
	}else{
		printPublicPrivateRow("alertscope", $_SWIFT["language"]["alertscope"], $_SWIFT["language"]["desc_alertscope"], $rulescope);
	} 

	$options = array();
	$index = 0;
	$_alertactions = array(ALERT_NEWTICKET, ALERT_NEWREPLY, ALERT_NEWSTAFFREPLY, ALERT_MOVE, ALERT_FLAG, ALERT_NOTE, ALERT_STATUS, ALERT_EDIT, ALERT_PRIORITY, ALERT_ASSIGN, ALERT_MERGE, ALERT_DELETEPOST, ALERT_DELETETICKET);
	$_alerttext = array(ALERT_NEWTICKET => $_SWIFT["language"]["alert_newticket"], ALERT_NEWREPLY => $_SWIFT["language"]["alert_newreply"], ALERT_NEWSTAFFREPLY => $_SWIFT["language"]["alert_newstaffreply"], ALERT_MOVE => $_SWIFT["language"]["alert_move"], ALERT_FLAG => $_SWIFT["language"]["alert_flag"], ALERT_NOTE => $_SWIFT["language"]["alert_note"], ALERT_STATUS => $_SWIFT["language"]["alert_status"], ALERT_EDIT => $_SWIFT["language"]["alert_edit"], ALERT_PRIORITY => $_SWIFT["language"]["alert_priority"], ALERT_ASSIGN => $_SWIFT["language"]["alert_assign"], ALERT_MERGE => $_SWIFT["language"]["alert_merge"], ALERT_DELETEPOST => $_SWIFT["language"]["alert_deletepost"], ALERT_DELETETICKET => $_SWIFT["language"]["alert_deleteticket"]);
	foreach ($_alertactions as $key=>$val)
	{
		$options[$index]["title"] = $_alerttext[$val];
		$options[$index]["value"] = $val;
		$index++;
	}

	printCheckboxListRow("alertactions", $_SWIFT["language"]["alertactions"], $_SWIFT["language"]["desc_alertactions"], $options);

	printDescRow($_SWIFT["language"]["alertoptions"]);
	printYesNoRow("execassign", $_SWIFT["language"]["execassign"], $_SWIFT["language"]["desc_execassign"], iif(empty($_POST["execassign"]), false, $_POST["execassign"]));

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["ralldepartments"];
	$options[0]["value"] = "0";
	$options[0]["selected"] = true;
	$index = 1;
	$departmentidlist = getAssignedDepartments(MODULE_TICKETS);
	foreach ($_SWIFT["departmentcache"] as $key=>$val)
	{
		if ($val["departmentmodule"] == MODULE_TICKETS && in_array($val["departmentid"], $departmentidlist))
		{
			$options[$index]["title"] = $val["title"];
			$options[$index]["value"] = $val["departmentid"];
			$index++;
		}
	}
	printSelectRow("departmentid", $_SWIFT["language"]["rdepartment"], $_SWIFT["language"]["desc_rdepartment"], $options);

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["rallticketstatus"];
	$options[0]["value"] = "0";
	$options[0]["selected"] = true;
	$index = 1;
	foreach ($_SWIFT["statuscache"] as $key=>$val)
	{
		$options[$index]["title"] = $val["title"];
		$options[$index]["value"] = $val["ticketstatusid"];
		$index++;
	}
	printSelectRow("ticketstatusid", $_SWIFT["language"]["rticketstatus"], $_SWIFT["language"]["desc_rticketstatus"], $options);

	$options = array();
	$options[0]["title"] = $_SWIFT["language"]["rallticketpriorities"];
	$options[0]["value"] = "0";
	$options[0]["selected"] = true;
	$index = 1;
	foreach ($_SWIFT["prioritycache"] as $key=>$val)
	{
		$options[$index]["title"] = $val["title"];
		$options[$index]["value"] = $val["priorityid"];
		$index++;
	}
	printSelectRow("priorityid", $_SWIFT["language"]["rticketpriority"], $_SWIFT["language"]["desc_rticketpriority"], $options);

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]));

	printMainTableFooter();

	addHiddenField("_m", "tickets");
	addHiddenField("_a", iif($type==INSERT, "insertalert", "editalert"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("alertruleid", $_POST["alertruleid"]);
	}
	printFormEnd();
}

/**
* Update existing alert rule
*/
function updateAlertRule($alertruleid, $title, $alerttype, $alertscope, $departmentid, $priorityid, $ticketstatusid, $execassign, $alertactions)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($alertactions))
	{
		return false;
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."alertrules` SET `alertscope` = '". intval($alertscope) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."', `departmentid` = '". intval($departmentid) ."', `priorityid` = '". intval($priorityid) ."', `ticketstatusid` = '". intval($ticketstatusid) ."', `alerttype` = '". intval($alerttype) ."', `execassign` = '". intval($execassign) ."', `title` = '". $dbCore->escape($title) ."' WHERE `alertruleid` = '". intval($alertruleid) ."';");

	deleteAllAlertActions(array($alertruleid));

	insertAlertActions($alertruleid, $alertactions);

	return true;
}

/**
* Insert a new Alert Rule
*/
function insertAlertRule($title, $alerttype, $alertscope, $departmentid, $priorityid, $ticketstatusid, $execassign, $alertactions)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($alertactions))
	{
		return false;
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."alertrules` (`alertscope`, `staffid`, `departmentid`, `priorityid`, `ticketstatusid`, `alerttype`, `execassign`, `title`) VALUES ('". intval($alertscope) ."', '". intval($_SWIFT["staff"]["staffid"]) ."', '". intval($departmentid) ."', '". intval($priorityid) ."', '". intval($ticketstatusid) ."', '". intval($alerttype) ."', '". intval($execassign) ."', '". $dbCore->escape($title) ."');");
	$alertruleid = $dbCore->lastInsertId();
	if (!$alertruleid)
	{
		return false;
	}

	insertAlertActions($alertruleid, $alertactions);

	return $alertruleid;
}

/**
* Insert Alert Actions
*/
function insertAlertActions($alertruleid, $alertactions)
{
	global $dbCore;

	$_sql = array();
	foreach ($alertactions as $key=>$val)
	{
		$_sql[] = "('". intval($alertruleid) ."', '". intval($val) ."')";
	}

	if (count($_sql))
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."alertactions` (`alertruleid`, `actiontype`) VALUES ".implode(",", $_sql).";");
	}

	return true;
}

/**
* Deletes the given Alert Rules
*/
function deleteAlertRules($alertruleidlist)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($alertruleidlist))
	{
		return false;
	}

	// First get all alert rules
	$_finalalertruleidlist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."alertrules` WHERE `alertruleid` IN (". buildIN($alertruleidlist) .");");
	while ($dbCore->nextRecord())
	{
		if (($dbCore->Record["alertscope"] == 0 && $dbCore->Record["staffid"] == $_SWIFT["staff"]["staffid"]) || ($dbCore->Record["alertscope"] == 1 && ($_SWIFT["staff"]["isadmin"] == 1 || $_SWIFT["staff"]["tdelarule"] != "0")))
		{
			$_finalalertruleidlist[] = $dbCore->Record["alertruleid"];
		}
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."alertrules` WHERE `alertruleid` IN (". buildIN($_finalalertruleidlist) .");");

	deleteAllAlertActions($_finalalertruleidlist);

	return true;
}

/**
* Deletes all the given actions under a given rule
*/
function deleteAllAlertActions($alertruleidlist)
{
	global $dbCore;

	if (!_is_array($alertruleidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."alertactions` WHERE `alertruleid` IN(". buildIN($alertruleidlist) .");");

	return true;
}
?>